//
//  LSYNetworkTools.swift
//  LSYWeibo
//
//  Created by 刘诗岩 on 16/4/13.
//  Copyright © 2016年 刘诗岩. All rights reserved.
//

import UIKit
import AFNetworking

// 使用结构体来定义接口名称
struct API {
    // 线上环境
    static let onlineHost = "https://api.weibo.com/"
    // 测试环境  在上传AppStore之前 一定要取消使用这个环境
    static let devHost = "https://dev.api.weibo.com/"
    static let authrizon = "oauth2/access_token"
    static let access_token = "2/users/show.json"
    static let homepage = "2/statuses/home_timeline.json"
}


// 枚举值
enum LSYHttpMethods: String {
    
    case POST = "POST"
    case GET = "GET"
}

class LSYNetworkTools: AFHTTPSessionManager {

    static let sharedTools: LSYNetworkTools = {
       
        let tools = LSYNetworkTools(baseURL: NSURL(string: API.onlineHost))
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
}

//MARK: 登录和注册相关
extension LSYNetworkTools {
    //MARK: 加载用户token
    func loadAccessToken(code: String, finished: (result: AnyObject?, error: NSError?) -> ()) {
        //AFN中参数 以字典的形式传递
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type": "authorization_code","code":code,"redirect_uri":redirect_uri]
        //调用核心方法的
        request(.POST, urlString: API.authrizon, parameters: parameters, finished: finished)
    }
    
    //MARK: 加载用户的信息
    func loadUserInfo(token: AnyObject, uid: AnyObject,finished: (result: AnyObject?, error: NSError?) -> ()) {
        //字典数组中不能够添加nil
        let parameters = ["access_token": token, "uid": uid]
        request(.GET, urlString: API.access_token, parameters: parameters, finished: finished)
    }
}

//MARK: 首页数据相关
extension LSYNetworkTools {
    func loadHomePageData(token: String,max_id: Int64,since_id: Int64,finished: (result: AnyObject?, error: NSError?) -> ()) {
        var parameters = ["access_token": token]
        if max_id > 0 {
            //max_id - 1解决数据重复的问题
            parameters["max_id"] = "\(max_id)"
        }
        if since_id > 0 {
            parameters["since_id"] = "\(since_id)"
        }
        request(.GET, urlString: API.homepage, parameters: parameters, finished: finished)
    }
}





//MARK: 网络请求的核心方法 以后所以的get 和 post都走这个方法
extension LSYNetworkTools {
    //封装所有的网络请求方法 所有的网络请求都是通过这个方法和 AFN进行联系
    func request(method: LSYHttpMethods,urlString: String, parameters: AnyObject?, finished: (result: AnyObject?, error: NSError?) -> ()) {
        
        //swift中就不能够使用利用协议 欺骗Xcode
        //为了达到封装的目的 可以将相同的闭包 抽取出来,当做参数传递给AFN框架
        let success =  { (task: NSURLSessionDataTask, result: AnyObject?) -> Void in
            //执行成功的回调
            finished(result: result, error: nil)
        }
        
        //定义失败的回调
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            //执行失败的回调
            print(error)
            finished(result: nil, error: error)
        }
        
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
    }
    
}
